package com.lovo.ssc.controller;


import com.alibaba.fastjson.JSON;
import com.lovo.dto.ReturnOrderDto;
import com.lovo.ssc.bean.Returnfailed;
import com.lovo.ssc.bean.Returnorder;
import com.lovo.ssc.bean.Returnorderhandle;
import com.lovo.ssc.dto.HandleOrderDto;
import com.lovo.ssc.service.IReturnfailedService;
import com.lovo.ssc.service.IReturnorderService;
import com.lovo.ssc.service.IReturnorderhandleService;
import com.lovo.util.MQUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lovo
 * @since 2022-07-11
 */
@RestController
@RequestMapping("/returnorderhandle")
@Api(tags = "退货订单处理记录应用控制器005--------刘文龙")
@Slf4j
public class ReturnorderhandleController {
	@Autowired
	IReturnorderhandleService handleService;
	@Autowired
	IReturnfailedService failedService;
	@Autowired
	IReturnorderService returnorderService;
	@Autowired
	RabbitTemplate rabbitTemplate;

	@ApiOperation(value = "根据处理状态获取退货订单处理记录")
	@GetMapping("findHandleOrderDtoByState/{state}")
	@ApiImplicitParam(name = "state", value = "0--未处理，1--已处理")
	public List<HandleOrderDto> findHandleOrderByState(@PathVariable("state") String state) {

		return handleService.findHandleOrderByState(state);
	}


	@ApiOperation(value = "处理退货订单")
	@PutMapping("disagreeReturn/{handleid}/{reason}/{returnOrderCode}")
	@ApiImplicitParams(value = {
			@ApiImplicitParam(name = "handleid", value = "退货订单处理记录id", required = true),
			@ApiImplicitParam(name = "reason", value = "原因", required = false)
	}
	)
	public void disagreeReturn(@PathVariable int handleid,
	                           @PathVariable String reason,
	                           @PathVariable String returnOrderCode) {
		//根据id获取退货订单处理记录
		Returnorderhandle handle = handleService.getById(handleid);
		log.info(reason);



		//拒绝退货
		if (reason != null && reason.equals("null") == false && reason.equals("") == false && reason.equals("undifine") == false) {
			//将退货订单状态修改为退货失败（1）
			handleService.updateState(returnOrderCode,"1");
			//有原因reason则为拒绝退货
			//修改处理状态
			handle.setHandlestate("1");
			//添加拒绝退货记录
			Returnfailed failed = new Returnfailed();
			failed.setFkreturngoodscode(handle.getFkreturngoodscode());
			failed.setFkusername(handle.getFkusername());
			failed.setReason(reason);
			failedService.save(failed);


			handleService.saveOrUpdate(handle);
		} else {
			//没有原因reason则为同意退货
			//将退货订单状态修改为正在退货（2）
			handleService.updateState(returnOrderCode,"2");

			//修改处理状态
			handle.setHandlestate("2");
//			log.info("同意退货");
			//根据退货单号获取退货订单
			Returnorder returnorder = returnorderService.findByReturnGoodsCode(handle.getFkreturngoodscode());
			//根据商品编号和购买单号查询需要退货商品批次的集合
			List<String> list = returnorderService.findBatchByCommodityCodeAndReturnGoodsCode(returnorder.getCommoditycode(), returnorder.getOrdercode());
			//			组装退货订单dto
			ReturnOrderDto dto = new ReturnOrderDto();
			dto.setUsername(returnorder.getUsername());
			dto.setReturngoodscode(returnorder.getReturngoodscode());
			dto.setOrdercode(returnorder.getOrdercode());
			dto.setCommoditycode(returnorder.getCommoditycode());
			dto.setCommodityname(returnorder.getCommodityname());
			dto.setCommoditybatch(list);
			dto.setCommodityNumber(list.size());
			dto.setNorms(returnorder.getNorms());
			dto.setType(returnorder.getType());
			dto.setRetuenprice(returnorder.getRetuenprice());
			dto.setReturndate(returnorder.getReturndate());
			//通过MQ，将退货订单dto发送给审核系统
			rabbitTemplate.convertAndSend(MQUtil.returnOrderFanoutExchange,MQUtil.returnOrderQueue, JSON.toJSONString(dto));


			handleService.saveOrUpdate(handle);
		}
	}
}
